package com.sh.data.engine.domain.datadev.offline.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.data.engine.domain.datadev.offline.model.domain.OfflineDevApiDomain;
import com.sh.data.engine.domain.datadev.offline.model.enums.OfflineDevApiNodeTypeEnum;
import com.sh.data.engine.domain.datadev.offline.service.DatadevOfflineApiService;
import com.sh.data.engine.domain.integration.api.http.model.domain.TaskDomain;
import com.sh.data.engine.domain.integration.api.http.model.param.ApiHttpAuthParam;
import com.sh.data.engine.domain.integration.api.http.model.param.ApiHttpRequestBodyParam;
import com.sh.data.engine.domain.integration.api.http.model.param.TestApiHttpParam;
import com.sh.data.engine.domain.integration.api.taskconfig.model.domain.TaskConfigDomain;
import com.sh.data.engine.domain.integration.api.webservice.model.param.ApiWsAuthParam;
import com.sh.data.engine.domain.integration.api.webservice.model.param.ApiWsRequestBodyParam;
import com.sh.data.engine.domain.integration.api.webservice.model.param.TestApiWsParam;
import com.sh.data.engine.domain.util.ConvertUtil;
import com.sh.data.engine.repository.dao.datadev.DatadevOfflineApiMapper;
import com.sh.data.engine.repository.dao.datadev.entity.DatadevOfflineApiEntity;
import com.sh.data.engine.repository.dao.datadev.entity.OfflineDevApiHttpEntity;
import com.sh.data.engine.repository.dao.datadev.entity.OfflineDevApiPreviewEntity;
import com.sh.data.engine.repository.dao.datadev.entity.OfflineDevApiWsEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class DatadevOfflineApiServiceImpl
    extends ServiceImpl<DatadevOfflineApiMapper, DatadevOfflineApiEntity>
    implements DatadevOfflineApiService {

    @Override
    public DatadevOfflineApiEntity getDatadevOfflineApiNodeId(Long nodeId) {
        LambdaQueryWrapper<DatadevOfflineApiEntity> eq =
            new LambdaQueryWrapper<DatadevOfflineApiEntity>()
                .eq(DatadevOfflineApiEntity::getNodeId, nodeId);
        return getOne(eq);
    }

    @Override
    public OfflineDevApiDomain getApiTaskByNodeId(Long nodeId) {

        DatadevOfflineApiEntity offlineDevApiTask = this.getDatadevOfflineApiNodeId(nodeId);
        if (null == offlineDevApiTask) {
            return null;
        }
        OfflineDevApiDomain offlineDevApiDomain =
            Convert.convert(OfflineDevApiDomain.class, offlineDevApiTask);

        // preview data
        OfflineDevApiPreviewEntity offlineDevApiPreviewEntity =
            offlineDevApiTask.getOfflineDevApiPreviewEntity();
        TaskDomain.PreviewExpressRequest previewExpressRequest =
            ConvertUtil.copyProperties(
                offlineDevApiPreviewEntity, TaskDomain.PreviewExpressRequest.class);
        if (CollectionUtils.isNotEmpty(offlineDevApiPreviewEntity.getExpressionParamList())) {
            previewExpressRequest.setExpressionParamList(
                ConvertUtil.copyProperties(
                    offlineDevApiPreviewEntity.getExpressionParamList(),
                    TaskDomain.PreviewExpressRequest.ExpressionParam.class));
        }
        offlineDevApiDomain.setPreviewExpressRequest(previewExpressRequest);

        // task
        DatadevOfflineApiEntity.TaskConfigRequest taskConfigRequest =
            offlineDevApiTask.getTaskConfigRequest();

        TaskConfigDomain taskConfigDomain =
            ConvertUtil.copyProperties(taskConfigRequest, TaskConfigDomain.class);

        taskConfigDomain.setTargetFields(
            ConvertUtil.copyProperties(
                taskConfigRequest.getTargetFields(), TaskConfigDomain.TargetField.class));
        taskConfigDomain.setPartitions(
            ConvertUtil.copyProperties(
                taskConfigRequest.getPartitions(), TaskConfigDomain.PartitionField.class));
        taskConfigDomain.setFieldMappings(
            ConvertUtil.copyProperties(
                taskConfigRequest.getFieldMappings(), TaskConfigDomain.FieldMapping.class));
        offlineDevApiDomain.setTaskConfigDomain(taskConfigDomain);

        if (Objects.equals(
            offlineDevApiDomain.getApiNodeType(), OfflineDevApiNodeTypeEnum.WS.getNodeDesc())) {
            OfflineDevApiWsEntity offlineDevApiWsEntity = offlineDevApiTask.getOfflineDevApiWsEntity();
            TestApiWsParam testApiWsParam =
                ConvertUtil.copyProperties(offlineDevApiWsEntity, TestApiWsParam.class);
            // auth
            testApiWsParam.setApiWsAuthParam(
                ConvertUtil.copyProperties(
                    offlineDevApiWsEntity.getApiWsAuthParam(), ApiWsAuthParam.class));

            // body
            OfflineDevApiWsEntity.ApiHttpRequestBodyParam entityBodyParam =
                offlineDevApiWsEntity.getApiWsRequestBodyParam();
            if (null != entityBodyParam) {
                ApiWsRequestBodyParam apiWsRequestBodyParam = new ApiWsRequestBodyParam();
                apiWsRequestBodyParam.setContentType(entityBodyParam.getContentType());
                apiWsRequestBodyParam.setBody(entityBodyParam.getBody());
                if (CollectionUtils.isNotEmpty(entityBodyParam.getFormParams())) {
                    apiWsRequestBodyParam.setFormParams(
                        ConvertUtil.copyProperties(
                            entityBodyParam.getFormParams(), ApiWsRequestBodyParam.FormParam.class));
                }
                testApiWsParam.setApiWsRequestBodyParam(apiWsRequestBodyParam);
            }
            offlineDevApiDomain.setOfflineDevApiWsParam(testApiWsParam);
        } else {
            OfflineDevApiHttpEntity offlineDevApiHttpEntity =
                offlineDevApiTask.getOfflineDevApiHttpEntity();
            TestApiHttpParam testApiHttpParam =
                ConvertUtil.copyProperties(offlineDevApiHttpEntity, TestApiHttpParam.class);
            // auth
            testApiHttpParam.setApiHttpAuthParam(
                ConvertUtil.copyProperties(
                    offlineDevApiHttpEntity.getApiHttpAuthParam(), ApiHttpAuthParam.class));

            // body
            OfflineDevApiHttpEntity.ApiHttpRequestBodyParam entityBodyParam =
                offlineDevApiHttpEntity.getApiHttpRequestBodyParam();
            if (null != entityBodyParam) {
                ApiHttpRequestBodyParam apiHttpRequestBodyParam = new ApiHttpRequestBodyParam();
                apiHttpRequestBodyParam.setContentType(entityBodyParam.getContentType());
                apiHttpRequestBodyParam.setBody(entityBodyParam.getBody());
                if (CollectionUtils.isNotEmpty(entityBodyParam.getFormParams())) {
                    apiHttpRequestBodyParam.setFormParams(
                        ConvertUtil.copyProperties(
                            entityBodyParam.getFormParams(), ApiHttpRequestBodyParam.FormParam.class));
                }
                testApiHttpParam.setApiHttpRequestBodyParam(apiHttpRequestBodyParam);
            }
            offlineDevApiDomain.setOfflineDevApiHttpParam(testApiHttpParam);
        }
        return offlineDevApiDomain;
    }
}
